package com.spic.business.home.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spic.business.home.domain.ShortcutFunction;
import com.spic.business.home.mapper.ShortcutFunctionMapper;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 290512
 * @create 2022-01-13 17:36
 */
@Service
public class ShortcutFunctionService
{
    @Autowired
    private ShortcutFunctionMapper shortcutFunctionMapper;

    public List<ShortcutFunction> getShortcutFunctionList()
    {

        QueryWrapper<ShortcutFunction> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("user_id", SecurityUtils.getUserId()).or().isNull("user_id");
        sectionQueryWrapper.select().orderByDesc("sort");
        List<ShortcutFunction> shortcutFunctionList = shortcutFunctionMapper.selectList(sectionQueryWrapper);
        return shortcutFunctionList;
    }

    public AjaxResult insertShortcutFunction(ShortcutFunction shortcutFunction ){
        List<ShortcutFunction> shortcutFunctionList = new ArrayList<>();
        if(shortcutFunction.getName().equals("我的待办")){
            shortcutFunctionList.add(getTodos(shortcutFunction.getName()));
        }
        QueryWrapper<ShortcutFunction> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("user_id", SecurityUtils.getUserId());
        sectionQueryWrapper.eq("name",shortcutFunction.getName());
        List<ShortcutFunction> shortcutFunctions = shortcutFunctionMapper.selectList(sectionQueryWrapper);
        if(shortcutFunctions !=null && shortcutFunctions.size()>0){
            shortcutFunctionList.add(shortcutFunctions.get(0));
        }

        if(shortcutFunctionList!=null && shortcutFunctionList.size()>0){
            return  AjaxResult.error(201,"快捷功能已存在，添加失败");
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String routerPath = shortcutFunction.getRouterPath();
        if(routerPath.indexOf("/index")!=-1){
             routerPath = routerPath.replaceAll("/index","");
            shortcutFunction.setRouterPath(routerPath);
        }
        shortcutFunction.setId(uuid);
        shortcutFunction.setSort(1);
        shortcutFunction.setUserId(SecurityUtils.getUserId());
        shortcutFunction.setCreateBy(SecurityUtils.getUserId());
        shortcutFunction.setCreateTime(DateUtils.getNowDate());
        int rows = shortcutFunctionMapper.insert(shortcutFunction);
        return AjaxResult.success(rows);
    }

    /**
     * 获取我的代办 快捷功能
     * @return
     */
    public ShortcutFunction getTodos(String name){
        QueryWrapper<ShortcutFunction> sectionQueryWrapper = new QueryWrapper<>();
        sectionQueryWrapper.eq("name", name);
        List<ShortcutFunction> shortcutFunctionList = shortcutFunctionMapper.selectList(sectionQueryWrapper);
        if(shortcutFunctionList!=null && shortcutFunctionList.size()>0){
            return shortcutFunctionList.get(0);
        }
        return null;
    }
}
